package com.ys.web.yinsheng.controller;

import com.ys.utils.base.http.ResponseHandler;
import com.ys.utils.base.j2se.JsonUtil;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.redis.Redis;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.mobao.manager.BankManager;
import com.ys.web.mobao.model.po.BindCard;
import com.ys.web.mobao.model.po.SystemBankPO;
import com.ys.web.mobao.service.MobaoBindCardService;
import com.ys.web.yinsheng.model.RequestData;
import com.ys.web.yinsheng.util.ServiceEnum;
import com.ys.web.yinsheng.util.StringUtils;
import com.ys.web.yinsheng.util.YinShengClient;
import com.ys.web.yinsheng.util.YsKJEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: dhf
 * @ClassName: YinshengController
 * @Date: 2022/7/20 20:20
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/ys")
public class YinshengController {
    @Autowired
    private MobaoBindCardService mobaoBindCardService;


    /**
     * @作者: dhf
     * @名称:银盛进件创建
     * @param
     * @时间: 2022/7/21 15:39
     * @备注:
     * @返回值: java.lang.String
     */
    @RequestMapping("/jj/create")
    public String create(){


        return "";
    }

    /**
     * @作者: dhf
     * @名称:短信验证码确认
     * @param requestNo
     * @param authSn
     * @param code
     * @时间: 2022/7/21 21:48
     * @备注:
     * @返回值: java.lang.String
     */
    @RequestMapping("/sms/confirm")
    public String smsConfirm(String requestNo,String authSn,String code){
        String callback = "";
        // 组装业务参数
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestNo", requestNo);
        requestMap.put("authSn",authSn);//银盛商户号
        requestMap.put("smsCode",code);//绑卡功能
        RequestData requestData = new RequestData(requestMap);
        YinShengClient yinShengClient = new YinShengClient(ServiceEnum.WALLET_CONFIRM_VERIFY);
        String result = yinShengClient.requestStrApi(requestData);
        if(StringHandler.isEmpty(result)){
            SystemError.wrapBs("500","短信验证码确认失败");
        }
        if (StringUtils.isBlank(result)) {
            log.error("短信验证码确认数据为空");
            SystemError.wrapBs("500","短信验证码确认失败");
        }
        Map<String,Object> obj = JsonUtil.jsonToBean(result,Map.class);
        String code1 = StringHandler.getString(obj.get("code"),"");
        if(!code1.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛响应成功.getCode())){
            log.error("短信验证码确认错误:"+obj.get("msg"));
            SystemError.wrapBs("500","短信验证码确认失败");
        }
        Map<String,Object> bizResponseJson = JsonUtil.jsonNullToBean(JsonUtil.toJson(obj.get("bizResponseJson")),Map.class);
        /**
         * 业务类型，不同业务类型返回字段不同
         *         01-手机号修改 02-平台内转账  03-钱包账户提现 04-钱包绑卡
         *         05-钱包账户充值 06-钱包消费 07-结算方式变更 08-账户支付*/
        String businessType	= StringHandler.getString(bizResponseJson.get("businessType"));
        String subCode = StringHandler.getString(bizResponseJson.get("subCode"),"");
        if(businessType.equals("06") || (businessType.equals("05"))){
            if(!subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应成功.getCode()) && !subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应处理中.getCode())){
                log.error("短信验证码确认错误:"+bizResponseJson.get("subMsg"));
                if(StringHandler.getString(bizResponseJson.get("subMsg")).contains("已超过有效时间")){
                    SystemError.wrapBs("7001",bizResponseJson.get("subMsg")+"");
                }
                if(StringHandler.getString(bizResponseJson.get("subMsg")).contains("动态验证码不符")){
                    SystemError.wrapBs("7002",bizResponseJson.get("subMsg")+"");
                }
                SystemError.wrapBs("500",bizResponseJson.get("subMsg")+"");
            }
        }else{
            if(!subCode.equals(YsKJEnum.TRUSTEESHIPSTATUS.银盛业务响应成功.getCode())){
                log.error("短信验证码确认错误:"+bizResponseJson.get("subMsg"));
                if(StringHandler.getString(bizResponseJson.get("subMsg")).contains("已超过有效时间")){
                    SystemError.wrapBs("7001",bizResponseJson.get("subMsg")+"");
                }
                if(StringHandler.getString(bizResponseJson.get("subMsg")).contains("动态验证码不符")){
                    SystemError.wrapBs("7002",bizResponseJson.get("subMsg")+"");
                }
                SystemError.wrapBs("500",bizResponseJson.get("subMsg")+"");
            }
        }

        if(businessType.equals("01")){

        }
        if(businessType.equals("02")){
            return ResponseHandler.success();
        }
        if(businessType.equals("03")){
            return ResponseHandler.success("0","提现成功，处理中");
        }
        if(businessType.equals("04")){
            Map<String,Object> cardInfo = JsonUtil.jsonNullToBean(JsonUtil.toJson(bizResponseJson.get("cardInfo")),Map.class);
            String str = Redis.str.get(requestNo);
            if(StringHandler.isNotEmpty(str)){
                BindCard bindCard = JsonUtil.jsonToBean(str,BindCard.class);
                bindCard.cardType = StringHandler.getString(cardInfo.get("cardType"),"");
                bindCard.linkId = StringHandler.getString(cardInfo.get("linkId"),"");
                bindCard.bankName = StringHandler.getString(cardInfo.get("bankName"),"");
                SystemBankPO systemBankPO = BankManager.SystemBank(bindCard.bankName);
                if(StringHandler.isNotEmpty(systemBankPO))
                    bindCard.bankCode = systemBankPO.bankCode;
                mobaoBindCardService.saveUserBank(bindCard);
                return ResponseHandler.result("0","绑卡成功",bindCard.cardToken);
//                mobaoBindCardService.updateBindCard(requestNo,cardType,linkId,bank_name);
            }
        }
        if(businessType.equals("05")){
            return ResponseHandler.success("0","充值成功，处理中");
        }
        if(businessType.equals("06")){
            return ResponseHandler.success("0","消费成功，处理中");
        }
        if(businessType.equals("07")){

        }
        if(businessType.equals("08")){

        }
        return ResponseHandler.success();
    }

    /**
     * @作者: dhf
     * @名称:短信验证码重发
     * @param requestNo
     * @时间: 2022/7/21 21:46
     * @备注:
     * @返回值: java.lang.String
     */
    @RequestMapping("/sms/retransmission")
    public String smsRetransmission(String requestNo){
        // 组装业务参数
        Map<String,Object> requestMap = new HashMap<>();
        requestMap.put("requestNo", requestNo);
        RequestData requestData = new RequestData(requestMap);
        YinShengClient yinShengClient = new YinShengClient(ServiceEnum.RESEND_CODE);
        Map<String, Object> obj = yinShengClient.requestApi(requestData,"短信验证码重发");
        if(StringHandler.isEmpty(obj)){
            SystemError.wrapBs("500","短信验证码重发失败");
        }
        String authSn = StringHandler.getString(obj.get("authSn"));
        requestMap.put("authSn",authSn);//银盛商户号
        return ResponseHandler.success(requestMap);
    }

}
